package com.edfeff.nio.http;

import com.edfeff.nio.ReactorSimpleHttpServer;

import java.io.IOException;
import java.nio.channels.SocketChannel;

public class SocketOptionsProcessor implements Runnable {
    private final ReactorSimpleHttpServer server;
    private SocketChannel socket;

    public SocketOptionsProcessor(ReactorSimpleHttpServer server, SocketChannel socket) {
        this.socket = socket;
        this.server = server;
    }

    @Override
    public void run() {
        try {
            socket.configureBlocking(false);

            //todo socket其他配置
            socket.socket().setSoLinger(true, 100);
            socket.socket().setTcpNoDelay(false);
            socket.socket().setSoTimeout(1000);

            server.getPoller().register(socket);

        } catch (IOException e) {
            e.printStackTrace();
            try {
                socket.close();
            } catch (IOException ex) {
                e.printStackTrace();
            }
        }
    }
}
